/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.chl.da.controllerBeans;

import edu.chl.da.ejb.ctrl.IManageAccount;
import edu.chl.da.ejb.ctrl.IManageCustomer;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;

/*
 * Controller bean for add customer events.
 * @author Kristofer
 */

@ManagedBean(name = "AddCustomerBean")
@RequestScoped
public class AddCustomerBean {
    
    private String accountName;
    private String password;
    private String repeatPassword;
    private String email;
    private String fname;
    private String lname;
    private boolean success = false;
    private String errorMsg;

    @EJB
    IManageAccount ima;

    @EJB
    IManageCustomer imc;

    public AddCustomerBean() {
    }

    public void registerAccount(ActionEvent e)
            throws AbortProcessingException {

        if(accountName.equals("") || password.equals("")  || email.equals("") || fname.equals("")  || lname.equals("") ) {
            errorMsg = "Fyll i alla falt!";
            success = false;
            return;
        }
        else if (!(ima.checkNameAvailability(accountName))){
            errorMsg = "Anvandarnamn upptaget, valj annat anvandarnamn!";
            success = false;
            return;
        }
        else if (!(password.equals(repeatPassword))) {
            errorMsg = "Angivna losenord ej identiska, forsok igen!";
            success = false;
            return;
        }
        else {
            success = true;
            imc.registerNewCustomer(accountName, password, email, fname, lname);
        }
    }

    public String navigate() {
       
        if (success) {
            return "/views/home.jsf?faces-redirect=true";
        } else {
            // Adding a failure message. Possible to display all
            // messages using <h:messages> (ending s!!)
            // or just target a specific control using <h:message> (no s!).
            FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage(errorMsg));
            return null; //Same page;
        }
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setRepeatPassword(String repeatPassword) {
        this.repeatPassword = repeatPassword;
    }

    public String getAccountName() {
        return accountName;
    }

    public String getEmail() {
        return email;
    }

    public String getFname() {
        return fname;
    }

    public String getLname() {
        return lname;
    }

    public String getPassword() {
        return password;
    }

    public String getRepeatPassword() {
        return repeatPassword;
    }
}
